/* eslint-disable require-jsdoc */
export function hasClass (elem, name) {
  var
    classes = elem.className.split(" ");
  var has = false;

  classes.forEach(item => {
    if (item === name) {
      has = true;
      return false;
    }
  });

  return has;
}

export function addClass (elem, name) {
  if (hasClass(elem, name)) {
    return;
  }

  elem.className += " " + name;
}

export function delClass (elem, name) {
  if (!hasClass(elem, name)) {
    return;
  }
  var classes = elem.className.split(" ");

  classes.splice(classes.indexOf(name), 1);
}

export function isChild (parent, child) {
  while (child) {
    if (child === parent) {
      return true;
    }
    child = child.parentNode;
  }
  return false;
}

/**
 * 检验请求检验是否正确
 * @param {Number} query 参数
 * @returns {Function} api
 */

export function checkStatus (res, status = 200, code = 0) {
  let isHasRes = res && res !== null && Object.keys(res).length > 0;
  if (!isHasRes) {
    return false;
  }
  if (res.status && res.status === status && res.data && res.data.code === code) {
    return true;
  }
  return false;
}
